//
//  KindsCollectionViewController.m
//  OpenYourEyes
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 zhanglei. All rights reserved.
//

#import "KindsCollectionViewController.h"
#import "KindsCollectionViewCell.h"
#import "PKRequestManager.h"
#import "CategoryVideo.h"
#import "CustomCell.h"
#import "KindsInfoController.h"
#import "Header.h"


@interface KindsCollectionViewController ()<MBProgressHUDDelegate>
{
    MBProgressHUD *_HUD;  //  数据加载菊花
}

/** 存储解析的数据*/
@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation KindsCollectionViewController
/**
 *  懒加载
 *
 *  @return 数据
 */
- (NSMutableArray *)array
{
    if (_array == nil)
    {
        self.array = [NSMutableArray array];
    }
    return _array;
}
/**
 *  数据加载菊花控件初始化
 */
- (void)addMBProgressHUD
{
    
    //初始化菊花加载
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //设置代理
    _HUD.delegate = self;
    //如果设置此属性则当前的view置于后台
    _HUD.dimBackground = YES;
    //设置对话框文字
    _HUD.labelText = @"请稍等";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addData];
    [self registerClass];
    [self deleteRightBarButton];
    
}
/**
 *  注册类
 */
- (void)registerClass
{
    [self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"before"];
}
/**
 *  去掉左边导航栏的back
 */
- (void)deleteRightBarButton
{
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                    forBarMetrics:UIBarMetricsDefault];
}
/**
 *  添加数据
 */
- (void)addData
{
    [PKRequestManager manager:POST requestWith:@"http://baobab.wandoujia.com/api/v1/categories?" parDic:nil finish:^(NSData *data)
    {
        //数据加载出来隐藏菊花
        [_HUD hide:YES];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *dic in array)
        {
            CategoryVideo *model = [[CategoryVideo alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        //刷新数据
        [self.collectionView reloadData];
    } error:^(NSError *error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"before" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    CategoryVideo *model = self.array[indexPath.row];
    cell.categoryVideo = model;
    return cell;    
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryVideo *model = self.array[indexPath.row];
    if (_kindsBlock != nil)
    {
        _kindsBlock(model);
    }
}
@end
